package cn.credit.loan.quartz;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
 * 实现任务调度执行
 * @author
 *
 */

@Configuration
public class SchedledConfiguration {
	/**
	 * 定义jobDetail.
	 * @param scheduledTasks
	 * @return
	 */
	@Bean
	public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduledTasks scheduledTasks){
		MethodInvokingJobDetailFactoryBean bean=new MethodInvokingJobDetailFactoryBean();
		//设置对应的job对象
		bean.setTargetObject(scheduledTasks);
		//设置scheduledTasks对象对应的方法名
		bean.setTargetMethod("excute");
		return bean;
	}
	
	/**
	 * 定义 Trigger.
	 * @param jobdetail
	 * @return
	 */
	@Bean
	public CronTriggerFactoryBean cronTriggerFactoryBean(MethodInvokingJobDetailFactoryBean jobdetail){
		CronTriggerFactoryBean trigger =new CronTriggerFactoryBean();
		trigger.setJobDetail(jobdetail.getObject());
		//每30秒执行一次
		trigger.setCronExpression("0/30 * * ? * *");
		return trigger;
	}
	

	/**
	 * 定义 scheduler.
	 * @param cronTriggerFactoryBean
	 * @return
	 */
	@Bean
	public SchedulerFactoryBean schedulerFactory(CronTriggerFactoryBean cronTriggerFactoryBean){
		SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean();
		schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
		return schedulerFactoryBean;
	}
}
